package lt.marsrutai.mobile.android.adapter;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.model.Schedule;
import lt.marsrutai.mobile.android.widget.ListViewAdapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ScheduleAdapter extends ListViewAdapter<Schedule> {

    private String scopeFolder;
    
    public ScheduleAdapter(Context context, String scopeFolder) {
        super(context, R.layout.schedule_item);
        
        this.scopeFolder = scopeFolder;
    }

    @Override
    public View doGetView(int position, View view, ViewGroup parent) {

        Schedule o = getItem(position);

        if (o != null) {
            
            view = inflater.inflate(R.layout.schedule_item, null);

            TextView scheduleTrackName = (TextView) view.findViewById(R.id.schedule_track_name);            

            TextView scheduleName = (TextView) view.findViewById(R.id.schedule_name);            
            scheduleName.setBackgroundColor(AppContext.getTransportInfoResolver().resolveTransportColor(scopeFolder, o.getTransportId()));
            
            if (null != o.getName() && o.getName().contains(o.getTrackName())) {
                scheduleTrackName.setText(o.getName());
            }
            else if (null != o.getTrackName() && o.getTrackName().contains(o.getName())) {
                scheduleTrackName.setText(o.getTrackName());
            }
            else {
                scheduleTrackName.setText(o.getTrackName());
                
                if (null != o.getName() && !o.getName().equalsIgnoreCase(o.getTrackName())) {
                    scheduleName.setText(o.getName());
                }
            }
            
            TextView validComment = (TextView) view.findViewById(R.id.valid_comment);
            if (null != validComment) {
                if (null != o.getValidComment()) {
                    validComment.setText(activity.getString(R.string.is_valid) + ' ' + o.getValidComment());
                }
                else {
                    validComment.setVisibility(View.GONE);
                }
            }

        }
        return view;
    }

}
